package com.HuangJiaQing.www.service.impl;

import com.HuangJiaQing.www.dao.*;
import com.HuangJiaQing.www.model.*;
import com.HuangJiaQing.www.service.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HONOR
 * @date 2022/7/27 22:23
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MarketersMapper marketersMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CourierMapper courierMapper;
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private HttpSession session;


    public String login(Model model, String name, String password, String login) {
        if ("user".equals(login)) {
            UserExample userExample = new UserExample();
            userExample.createCriteria().andUsernameEqualTo(name).andPasswordEqualTo(password);
            List<User> users = userMapper.selectByExample(userExample);
            if (users.isEmpty()) {
                return "login";
            } else {
                session.setAttribute("user", users.get(0));
                List<Marketers> marketers = marketersMapper.selectByExample(null);
                model.addAttribute("marketers",marketers);
                return "user";
            }
        } else if ("marketers".equals(login)) {
            MarketersExample marketersExample = new MarketersExample();
            marketersExample.createCriteria().andMarketersNameEqualTo(name).andMarketersPasswordEqualTo(password);
            List<Marketers> marketers = marketersMapper.selectByExample(marketersExample);
            if (marketers.isEmpty()||marketers.get(0).getIsForbid()>0) {
                return "login";
            } else {
                session.setAttribute("marketers", marketers.get(0));
                GoodsExample goodsExample = new GoodsExample();
                goodsExample.createCriteria().andMarketersNameEqualTo(marketers.get(0).getMarketersName());
                List<Goods> goods = goodsMapper.selectByExample(goodsExample);
                model.addAttribute("goods",goods);
                return "marketers";
            }
        } else if("courier".equals(login)){
            CourierExample courierExample = new CourierExample();
            courierExample.createCriteria().andCourierNameEqualTo(name).andCourierPasswordEqualTo(password);
            List<Courier> couriers = courierMapper.selectByExample(courierExample);
            if (couriers.isEmpty()){
                return "login";
            }else {
                session.setAttribute("courier",couriers.get(0));
                BillExample billExample = new BillExample();
                billExample.createCriteria().andCourierNameEqualTo(couriers.get(0).getCourierName()).andIsCompleteEqualTo(0);
                List<Bill> bills = billMapper.selectByExampleWithGoods(billExample);
                model.addAttribute("bills",bills);
                return "courier";
            }

        }else if ("admin".equals(login)) {
            AdminExample adminExample = new AdminExample();
            adminExample.createCriteria().andAdminNameEqualTo(name).andAdminPasswordEqualTo(password);
            List<Admin> admins = adminMapper.selectByExample(adminExample);
            if (admins.isEmpty()) {
                return "login";
            } else {
                session.setAttribute("admin", admins.get(0));
                List<Marketers> marketers = marketersMapper.selectByExample(null);
                model.addAttribute("marketers",marketers);
                return "admin";
            }
        }
        return "login";
    }

    public List<Marketers> visit() {
        List<Marketers> marketers = marketersMapper.selectByExample(null);
        return marketers;
    }

}
